from django import forms
from django.core.validators import RegexValidator
from django.shortcuts import render

# Create your views here.
from app01 import models


class RegisterModelForm(forms.ModelForm):
    mobile_phone = forms.CharField(label="手机号",
                                   validators=[RegexValidator(r"^(1[3|4|5|6|7|8|9])\d{9}$", "手机号码格式错误")])
    password = forms.CharField(label="密码",
                               widget=forms.widgets.PasswordInput(attrs={"class": "form-control", "placeholder": "密码"})
                               )
    confirm_password = forms.CharField(label="确认密码",
                                       widget=forms.widgets.PasswordInput(
                                           attrs={"class": "form-control", "placeholder": "确认密码"}))

    class Meta:
        model = models.UserInfo
        # fields = '__all__'
        # 如果想要前端显示的顺序按照自己的来，这里不能用all
        fields = ['username', 'email', 'mobile_phone', "password", 'confirm_password']

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        # self.field 就是上面的所有字段,有序字典
        for name, field in self.fields.items():
            field.widget.attrs['class'] = 'form-control'
            field.widget.attrs['placeholder'] = '请输入%s' % (field.label)


def register(request):
    form = RegisterModelForm()
    return render(request, 'app01/register.html', {"form": form})
